package org.god.batis.core;

import java.util.HashMap;
import java.util.Map;

public class SqlSessionFactory {
    /**
     * 事务管理器
     * 时可以灵活切换的
     */
    private Transaction transaction;
    /**
     * 存放SQL的map集合
     * key:namespace+id
     * value:MappedStatement
     */
    private Map<String, MappedStatement> mappedStatementMap = new HashMap<>();

    public SqlSessionFactory() {
    }

    public SqlSessionFactory(Transaction transaction, Map<String, MappedStatement> mappedStatementMap) {
        this.transaction = transaction;
        this.mappedStatementMap = mappedStatementMap;
    }

    public void setTransaction(Transaction transaction) {
        this.transaction = transaction;
    }

    public void setMappedStatementMap(Map<String, MappedStatement> mappedStatementMap) {
        this.mappedStatementMap = mappedStatementMap;
    }

    public Transaction getTransaction() {
        return transaction;
    }

    public Map<String, MappedStatement> getMappedStatementMap() {
        return mappedStatementMap;
    }

    public SqlSession openSession() {
        //开启连接
        transaction.openCollection();
        SqlSession sqlSession = new SqlSession(this);
        return sqlSession;
    }

    @Override
    public String toString() {
        return "SqlSessionFactory{" +
                "transaction=" + transaction +
                ", mappedStatementMap=" + mappedStatementMap +
                '}';
    }
}
